﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AllenData;
using AllenNeighborhood.UI;

namespace AllenNeighborhood
{
    public partial class NeighborAdd : System.Web.UI.Page
    {        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Request.QueryString["StreetNumber"] != null)
            {
                this.StreetNumber = this.Request.QueryString["StreetNumber"];
            }

            if (this.Request.QueryString["StreetID"] != null)
            {
                this.StreetID = Utilities.ParseScreenInt(this.Request.QueryString["StreetID"]);
            }

            if (this.Request.QueryString["NeighborID"] != null)
            {
                this.NeighborID = Utilities.ParseScreenInt(this.Request.QueryString["NeighborID"]);
            }

            if (!this.IsPostBack)
            {
                ShowStreetNames();
                ShowGenders();
                ShowEthnicities();
                ShowHomeOwnerStatus();
                ShowCoverages();
                ShowEnrollmentStatus();

                if (this.NeighborID != null)
                {
                    this.LoadNeighbor();
                    this.MessageText.Text = "Update a neighbor";
                    this.NeighborAddButton.Text = "Update";
                }
                else
                {
                    this.MessageText.Text = "Add a new neighbor";
                }

                if (this.StreetNumber != null)
                {
                    this.HouseNumberBox.Text = this.StreetNumber;
                    this.StreetDropDown.SelectedValue = this.StreetID.ToString();
                }
            }
        }

        public int? NeighborID
        {
            get;
            set;
        }

        public string StreetNumber
        {
            get;
            set;
        }

        public int StreetID
        {
            get;
            set;
        }

        protected void LoadNeighbor()
        {
            var data = new NeighborData();
            Neighbor neighbor = data.GetNeighbor(this.NeighborID.GetValueOrDefault());
            if (neighbor == null)
            {
                return;
            }

            this.FirstNameBox.Text = neighbor.FirstName;
            this.LastNameBox.Text = neighbor.LastName;
            this.HouseNumberBox.Text = neighbor.StreetNumber;

            int streetID = neighbor.StreetID.GetValueOrDefault();
            if (streetID > 0)
            {
                this.StreetDropDown.SelectedValue = streetID.ToString();
            }

            this.ZipCodeBox.Text = neighbor.Zip;
            this.PhoneNumberBox.Text = neighbor.PhoneNumber;
            this.EmailBox.Text = neighbor.EmailAddress;
            this.GenderDropDown.SelectedValue = neighbor.GenderID.ToString();
            this.EthnicityDropDown.SelectedValue = neighbor.EthnicityID.ToString();
            this.AgeBox.Text = neighbor.Age;
            this.OwnHomeDropDown.SelectedValue = neighbor.HomeOwnerStatusID.ToString();
            this.CoverageDropDown.SelectedValue = neighbor.InsuranceCoverageID.ToString();

            int enrollmentStatusID = neighbor.EnrollmentStatusID.GetValueOrDefault();
            if (enrollmentStatusID > 0)
            {
                this.NewlyEnrolledDropDown.SelectedValue = enrollmentStatusID.ToString();
            }

            this.IHPorMEDDropDown.Text = neighbor.IhpMed;
            this.DateEnrolledBox.Text = neighbor.EnrollmentDate == null ? null : neighbor.EnrollmentDate.GetValueOrDefault().ToString();
            this.OutreachWorkerBox.Text = neighbor.OutreachName;
            this.KidsCount.Text = neighbor.Kids == null ? "0" : neighbor.Kids.GetValueOrDefault().ToString();
            this.KidsAgesBox.Text = neighbor.KidsAges;
            this.BestContactMethod.Text = neighbor.BestContactMethod;
        }

        protected void ShowEnrollmentStatus()
        {
            var data = new EnrollmentStatusData();
            List<EnrollmentStatus> enrollment = data.GetEnrollmentStatuses();
            enrollment = (from i in enrollment orderby i.Name select i).ToList();

            NewlyEnrolledDropDown.DataSource = enrollment;
            NewlyEnrolledDropDown.DataValueField = "EnrollmentStatusID";
            NewlyEnrolledDropDown.DataTextField = "Name";
            NewlyEnrolledDropDown.DataBind();
        }

        protected void ShowStreetNames()
        {   
            var data = new StreetData();
            List<Street> streets = data.GetAllStreets().OrderBy(c => c.Name).ToList();

            StreetDropDown.DataSource = streets;
            StreetDropDown.DataValueField = "StreetID";
            StreetDropDown.DataTextField = "Name";
            StreetDropDown.DataBind();
        }

        protected void ShowGenders()
        {
            var data = new GenderData();
            List<Gender> genders = data.GetAllGenders().OrderBy(c => c.Name).ToList();

            GenderDropDown.DataSource = genders;
            GenderDropDown.DataValueField = "GenderID";
            GenderDropDown.DataTextField = "Name";
            GenderDropDown.DataBind();
        }

        protected void ShowEthnicities()
        {
            var data = new EthnicityData();
            List<Ethnicity> ethnicities = data.GetAllEthnicities().OrderBy(c => c.Name).ToList();

            EthnicityDropDown.DataSource = ethnicities;
            EthnicityDropDown.DataValueField = "EthnicityID";
            EthnicityDropDown.DataTextField = "Name";
            EthnicityDropDown.DataBind();
        }

        protected void ShowHomeOwnerStatus()
        {
            var data = new HomeOwnerStatusData();
            List<HomeOwnerStatus> homeOwnerStatuses = data.GetAllhomeOwnerStatuses().OrderBy(c => c.Name).ToList();

            OwnHomeDropDown.DataSource = homeOwnerStatuses;
            OwnHomeDropDown.DataValueField = "HomeOwnerStatusID";
            OwnHomeDropDown.DataTextField = "Name";
            OwnHomeDropDown.DataBind();
        }

        protected void ShowCoverages()
        {
            var data = new InsuranceCoverageData();
            List<InsuranceCoverage> coverages = data.GetAllInsuranceCoverages().OrderBy(c => c.Name).ToList();

            CoverageDropDown.DataSource = coverages;
            CoverageDropDown.DataValueField = "InsuranceCoverageID";
            CoverageDropDown.DataTextField = "Name";
            CoverageDropDown.DataBind();
        }

        protected void NeighborAddButton_Click(object sender, EventArgs e)
        {
            var dal = new NeighborData();
            
            Neighbor data = null;
            if (this.NeighborID != null)
            {
                data = dal.GetNeighbor(this.NeighborID.GetValueOrDefault());
            }
            else
            {
                data = new Neighbor();
            }

            data.Age = this.AgeBox.Text;
            data.BestContactMethod = this.BestContactMethod.Text;
            data.EmailAddress = this.EmailBox.Text;
            data.EnrollmentDate = Utilities.ParseScreenDate(this.DateEnrolledBox.Text);
            data.EnrollmentStatusID = Utilities.ParseScreenInt(this.NewlyEnrolledDropDown.SelectedValue);
            data.EthnicityID = Utilities.ParseScreenInt(this.EthnicityDropDown.SelectedValue);
            data.FirstName = this.FirstNameBox.Text;
            data.GenderID = Convert.ToInt32(this.GenderDropDown.SelectedValue);
            data.HomeOwnerStatusID = Utilities.ParseScreenInt(this.OwnHomeDropDown.SelectedValue);
            data.IhpMed = this.IHPorMEDDropDown.Text;
            data.InsuranceCoverageID = Utilities.ParseScreenInt(this.CoverageDropDown.SelectedValue);
            data.Kids = Utilities.ParseScreenInt(this.KidsCount.Text);
            data.KidsAges = this.KidsAgesBox.Text;
            data.LastName = this.LastNameBox.Text;
            data.EnrollmentStatusID =  Utilities.ParseScreenInt(this.NewlyEnrolledDropDown.SelectedValue);
            data.OutreachName = this.OutreachWorkerBox.Text;
            data.PhoneNumber = this.PhoneNumberBox.Text;
            data.StreetID = Utilities.ParseScreenInt(this.StreetDropDown.SelectedValue);
            data.StreetNumber = this.HouseNumberBox.Text;
            data.Zip = this.ZipCodeBox.Text;

            if (this.NeighborID != null)
            {
                dal.UpdateNeighbor(data);
                this.Response.Redirect(string.Format("~/Secure/NeighborList.aspx?StreetNumber={0}&StreetID={1}", data.StreetNumber, data.StreetID));
            }
            else
            {
                int neighborID = dal.AddNewNeighbor(data);
                this.Response.Redirect(string.Format("~/Secure/InteractionAdd.aspx?NeighborID={0}", neighborID));
            }
        }
    }
}
